/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hashChunk;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author Chickyky
 */
public class TorrentFile {

    private JList m_ListGUI;
    private ArrayList<Chunk> m_ListChunk;
    private DefaultListModel listModel;
    private long n;
    private String extFile;

    public String getExtFile() {
        return extFile;
    }

    public void setExtFile(String extFile) {
        this.extFile = extFile;
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
    }

    public ArrayList<Chunk> getM_ListChunk() {
        return m_ListChunk;
    }

    public void setM_ListChunk(ArrayList<Chunk> m_ListChunk) {
        this.m_ListChunk = m_ListChunk;
    }

    public JList getM_ListGUI() {
        return m_ListGUI;
    }

    public void setM_ListGUI(JList m_ListGUI) {
        this.m_ListGUI = m_ListGUI;
    }

    public long getN() {
        return n;
    }

    public void setN(long n) {
        this.n = n;
    }

    public TorrentFile(JList m_ListGUI) {
        this.m_ListGUI = m_ListGUI;
    }

    public Chunk getChunkInList(String chunkName) {
        for (Chunk chunk : getM_ListChunk()) {
            if (chunk.getM_FileName().equals(chunkName)) {
                return chunk;
            }
        }
        return null;
    }

//    public void removeChunkInList(File torrentFile, Chunk oldChunk) {
//        for (Chunk chunk : getM_ListChunk()) {
//            if (chunk.getM_FileName().equals(oldChunk.getM_FileName())
//                    && chunk.getM_HashValue().equals(oldChunk.getM_HashValue())) {
//                m_ListChunk.remove(chunk);
//                setM_ListChunk(m_ListChunk);
//                this.readFileTorrent(torrentFile);
//            }
//        }
//    }

    private boolean checkChunkInList(File folder, Chunk newChunk) {
        File[] files = folder.listFiles();
        for (File file : files) {
            if (file.getName().equals(newChunk.getM_FileName())) {
                return false;
            }
        }
        return true;
    }

    public void readFileTorrent(File torrentFile) {
        if (this.m_ListChunk == null) {
            this.m_ListChunk = new ArrayList<>();
        } else {
            this.m_ListChunk.clear();
        }

        String fileName;
        String sourceFName = torrentFile.getName(); //lấy tên file bao gồm cả phần mở rộng
        sourceFName = sourceFName.substring(0, sourceFName.lastIndexOf(".")); //lấy tên file ko chưa phần mở rộng

        File folder = new File("share\\download\\" + sourceFName); //tạo folder  lưu các file Chunk
        if (!folder.exists()) {
            folder.mkdir();
        } else {
        }

        try {
            BufferedReader input = new BufferedReader(new FileReader(torrentFile));
            String line = "";
            this.n = Long.parseLong(input.readLine());
            this.extFile = input.readLine();
            while ((line = input.readLine()) != null) {
                String[] str = line.split(";");
                Chunk chunk = new Chunk();
                chunk.setM_FileName(str[0]);
                chunk.setM_HashValue(str[1]);
                if (checkChunkInList(folder, chunk)) {
                    this.m_ListChunk.add(chunk);
                }
            }
            input.close();
        } catch (IOException | NumberFormatException ex) {
            ex.printStackTrace();
            System.out.println("dọc file node bị lỗi");
        }
        this.updateListFileTorrentOntoGUI();
    }

    public void updateListFileTorrentOntoGUI() {
        if (getListModel() == null) {
            setListModel(new DefaultListModel());
            this.m_ListGUI.setModel(getListModel());
        } else {
            getListModel().clear();
        }
        for (Chunk item : this.getM_ListChunk()) {
            getListModel().addElement(item.getM_FileName());
        }
    }

    public void reset() {
        m_ListChunk.clear();
        extFile = "";
        n = 0;
        listModel = null;
        setListModel(listModel);

        updateListFileTorrentOntoGUI();
    }
}
